package cn.gzjp.modules.wx.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.util.List;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import cn.gzjp.common.utils.DateUtils;
import cn.gzjp.common.utils.MyBeanUtils;
import cn.gzjp.common.persistence.Page;
import cn.gzjp.common.web.BaseController;
import cn.gzjp.common.utils.StringUtils;
import cn.gzjp.common.utils.excel.ExportExcel;
import cn.gzjp.common.utils.excel.ImportExcel;
import cn.gzjp.modules.wx.entity.WxMpAccount;
import cn.gzjp.modules.wx.service.WxMpAccountService;

/**
 * 小程序配置信息Controller
 * @author zengcp
 * @version 2018-04-03
 */
@Controller
@RequestMapping(value = "${adminPath}/wx/wxMpAccount")
public class WxMpAccountController extends BaseController {

	@Autowired
	private WxMpAccountService wxMpAccountService;
	
	
	/**
	 * 小程序配置信息列表页面
	 */
	@RequiresPermissions("wx:wxMpAccount:list")
	@RequestMapping(value = {"list", ""})
	public String list(WxMpAccount wxMpAccount, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WxMpAccount> page = wxMpAccountService.findPage(new Page<WxMpAccount>(request, response), wxMpAccount); 
		model.addAttribute("page", page);
		return "modules/wx/wxMpAccountList";
	}

	/**
	 * 查看，增加，编辑小程序配置信息表单页面
	 */
	@RequiresPermissions(value={"wx:wxMpAccount:view","wx:wxMpAccount:add","wx:wxMpAccount:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(WxMpAccount wxMpAccount, Model model) {
		model.addAttribute("wxMpAccount", wxMpAccount);
		return "modules/wx/wxMpAccountForm";
	}

	/**
	 * 保存小程序配置信息
	 */
	@RequiresPermissions(value={"wx:wxMpAccount:add","wx:wxMpAccount:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(WxMpAccount wxMpAccount, Model model, RedirectAttributes redirectAttributes) throws Exception{
		wxMpAccountService.save(wxMpAccount);//保存
		addMessage(redirectAttributes, "保存小程序配置信息成功");
		return redirect("/wx/wxMpAccount/?repage");
	}
	
	/**
	 * 删除小程序配置信息
	 */
	@RequiresPermissions("wx:wxMpAccount:del")
	@RequestMapping(value = "delete")
	public String delete(WxMpAccount wxMpAccount, RedirectAttributes redirectAttributes) {
		wxMpAccountService.delete(wxMpAccount);
		addMessage(redirectAttributes, "删除小程序配置信息成功");
		return redirect("/wx/wxMpAccount/?repage");
	}
	
	/**
	 * 批量删除小程序配置信息
	 */
	@RequiresPermissions("wx:wxMpAccount:del")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			wxMpAccountService.delete(wxMpAccountService.get(id));
		}
		addMessage(redirectAttributes, "删除小程序配置信息成功");
		return redirect("/wx/wxMpAccount/?repage");
	}
	
	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("wx:wxMpAccount:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(WxMpAccount wxMpAccount, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "小程序配置信息"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<WxMpAccount> page = wxMpAccountService.findPage(new Page<WxMpAccount>(request, response, -1), wxMpAccount);
    		new ExportExcel("小程序配置信息", WxMpAccount.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出小程序配置信息记录失败！失败信息："+e.getMessage());
		}
		return redirect("/wx/wxMpAccount/?repage");
    }

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("wx:wxMpAccount:import")
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<WxMpAccount> list = ei.getDataList(WxMpAccount.class);
			for (WxMpAccount wxMpAccount : list){
				try{
					wxMpAccountService.save(wxMpAccount);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条小程序配置信息记录。");
			}
			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条小程序配置信息记录"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入小程序配置信息失败！失败信息："+e.getMessage());
		}
		return redirect("/wx/wxMpAccount/?repage");
    }
	
	/**
	 * 下载导入小程序配置信息数据模板
	 */
	@RequiresPermissions("wx:wxMpAccount:import")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "小程序配置信息数据导入模板.xlsx";
    		List<WxMpAccount> list = Lists.newArrayList(); 
    		new ExportExcel("小程序配置信息数据", WxMpAccount.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return redirect("/wx/wxMpAccount/?repage");
    }
	@ModelAttribute
	public WxMpAccount get(@RequestParam(required=false) String id) {
		WxMpAccount entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = wxMpAccountService.get(id);
		}
		if (entity == null){
			entity = new WxMpAccount();
		}
		return entity;
	}
	

}